package controller

import (
	"fmt"
	"github.com/afex/hystrix-go/hystrix"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"myweb/web/proto/order"
	"myweb/web/utils"
	"net/http"
)

type OrderRequest struct {
	HouseId   string `json:"house_id"`
	StartDate string `json:"start_date"`
	EndDate   string `json:"end_date"`
}

func PostOrders(ctx *gin.Context) {
	var OrderInfo OrderRequest
	err := ctx.ShouldBind(&OrderInfo)
	if err != nil {
		fmt.Println("parse orders info err: ", err)
		return
	}
	fmt.Println("---------OrderInfo-----------", OrderInfo)

	session := sessions.Default(ctx)
	userName := session.Get("userName").(string)
	fmt.Println("--------userName: ", userName)

	var resp = new(order.OrderResp)
	orderService := ctx.Keys["Order"].(order.OrderClient)
	err = hystrix.Do("order", func() error {
		var err error
		resp, err = orderService.PostOrders(ctx, &order.OrderReq{
			HouseId:   OrderInfo.HouseId,
			StartDate: OrderInfo.StartDate,
			EndDate:   OrderInfo.EndDate,
			UserName:  userName,
		})
		return err
	}, nil)

	if err != nil {
		fmt.Println("获取微服务Order， err: ", err)
		ResponseError(ctx, utils.RECODE_SERVERERR)
		return
	}

	ctx.JSON(http.StatusOK, resp)
}

// http://10.10.53.170:8080/api/v1.0/user/orders?role=custom
func GetOrders(ctx *gin.Context) {

	role := ctx.Query("role")
	fmt.Println("---------order role: ", role)

	session := sessions.Default(ctx)
	userName := session.Get("userName").(string)
	fmt.Println("--------userName: ", userName)

	var resp = new(order.GetResp)
	orderService := ctx.Keys["Order"].(order.OrderClient)
	err := hystrix.Do("order", func() error {
		var err error
		resp, err = orderService.GetOrders(ctx, &order.GetReq{
			Role:     role,
			UserName: userName,
		})
		return err
	}, nil)

	if err != nil {
		fmt.Println("获取微服务Order， err: ", err)
		ResponseError(ctx, utils.RECODE_SERVERERR)
		return
	}

	//ctx.JSON(http.StatusOK, resp)
	fmt.Println("orders:", resp)
	ResponseOK(ctx, utils.RECODE_OK, resp.Data)
}

// PutOrders 更新订单状态
// @Summary 更新订单状态
// @Description 更新订单状态
// @Tags 更新订单状态
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {string} string "信息"
// @Router /api/v1.0/orders/:id/status [PUT]

// {action: "accept"}

type StatusOrder struct {
	Action string `json:"action"`
	Reason string `json:"reason"`
}

// PutOrders 更新订单状态
// @Summary 更新订单状态
// @Description 更新订单状态
// @Tags 更新订单状态
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {string} string "信息"
// @Router /api/v1.0/orders/:id/status [PUT]
func PutOrders(ctx *gin.Context) {
	id := ctx.Param("id")
	fmt.Println("----------id: ", id)

	var status StatusOrder
	err := ctx.ShouldBind(&status)
	if err != nil {
		fmt.Println("获取订单状态失败,err: ", err)
		return
	}
	fmt.Println("订单状态信息: ", status)

	var resp = new(order.UpdateResp)
	orderService := ctx.Keys["Order"].(order.OrderClient)
	err = hystrix.Do("order", func() error {
		var err error
		resp, err = orderService.UpdateStatus(ctx, &order.UpdateReq{
			Action: status.Action,
			Reason: status.Reason,
			Id:     id,
		})
		return err
	}, nil)

	if err != nil {
		fmt.Println("调用微服务Order， err: ", err)
		ResponseError(ctx, utils.RECODE_SERVERERR)
		return
	}

	if resp.Errno == utils.RECODE_DBERR {
		fmt.Println("订单处理失败:", err)
		ResponseError(ctx, utils.RECODE_DBERR)
		return
	}
	fmt.Println("------orderStatus: ", resp)
	ResponseOK(ctx, utils.RECODE_OK, resp)
}

type Comment struct {
	OrderId string `json:"order_id"`
	Comment string `json:"comment"`
}

// PutComment 订单评价
// @Summary 订单评价
// @Description 订单评价
// @Tags 订单评价
// @Accept json
// @Produce json
// @Param Comment body Comment true "Comment"
// @Success 200 {string} string "信息"
// @Router /api/v1.0/orders/:id/comment [PUT]
func PutComment(ctx *gin.Context) {
	var req Comment
	err := ctx.ShouldBind(&req)
	//校验数据
	if err != nil || req.OrderId == "" || req.Comment == "" {
		fmt.Println("获取数据错误", err)
		return
	}
	fmt.Println("------ comment: ", req)

	var resp = new(order.UpdateResp)
	orderService := ctx.Keys["Order"].(order.OrderClient)
	err = hystrix.Do("order", func() error {
		var err error
		resp, err = orderService.PutComment(ctx, &order.CommentReq{
			Orderid: req.OrderId,
			Comment: req.Comment,
		})
		return err
	}, nil)

	if err != nil {
		fmt.Println("调用微服务Order， err: ", err)
		ResponseError(ctx, utils.RECODE_SERVERERR)
		return
	}

	if resp.Errno == utils.RECODE_DBERR {
		fmt.Println("订单处理失败:", err)
		ResponseError(ctx, utils.RECODE_DBERR)
		return
	}
	fmt.Println("------orderComment: ", resp)
	ResponseOK(ctx, utils.RECODE_OK, resp)

}
